package controller

import (
	"gin/common"
	"gin/model"
	"gin/response"
	"github.com/gin-gonic/gin"
	"strings"
)

func AddAddress(ctx *gin.Context) {
	mobile := ctx.PostForm("mobile")
	isDefault := ctx.PostForm("isDefault") //是否默认地址
	linkMan := ctx.PostForm("linkMan")     //联系人
	latitude := ctx.PostForm("latitude")
	longitude := ctx.PostForm("longitude")
	provinceId := ctx.PostForm("provinceId")
	cityId := ctx.PostForm("cityId")
	districtId := ctx.PostForm("districtId")
	address := ctx.PostForm("address")
	if len(linkMan) == 0 || len(address) == 0 || len(provinceId) == 0 || len(cityId) == 0 {
		response.Fail(ctx, gin.H{}, "参数有误")
		return
	}
	Db := common.GetDB()
	user, _ := ctx.Get("user")
	if isDefault != "true" {
		isDefault = "false"
	} else {
		type changeDefault struct {
			IsDefault string `gorm:"column:isDefault"`
		}
		structDefault := changeDefault{
			IsDefault: "false",
		}
		Db.Debug().Table("cat_meal_address").Where("uid = ?", user.(model.UserInfo).Uid).Updates(&structDefault)
	}
	if len(mobile) != 11 {
		response.Fail(ctx, gin.H{}, "手机号码有误")
		return
	}
	insert := model.InsertAddressInfo{
		Uid:        user.(model.UserInfo).Uid,
		LinkMan:    linkMan,
		Address:    address,
		Mobile:     mobile,
		IsDefault:  isDefault,
		Latitude:   latitude,
		Longitude:  longitude,
		ProvinceId: provinceId,
		CityId:     cityId,
		DistrictId: districtId,
	}
	Db.Debug().Table("cat_meal_address").Create(&insert)
	response.Success(ctx, gin.H{}, "添加成功")
}

// AddressList 地址列表
func AddressList(ctx *gin.Context) {
	user, _ := ctx.Get("user")
	var list []*model.AddressInfo
	Db := common.GetDB()
	Db.Debug().Table("cat_meal_address").Where("uid =?", user.(model.UserInfo).Uid).Find(&list)
	for _, value := range list {
		var province, city, district Province
		Db.Table("cat_meal_region").Where("id = ?", value.ProvinceId).First(&province)
		value.ProvinceStr = province.Name
		Db.Table("cat_meal_region").Where("id = ?", value.CityId).First(&city)
		value.CityStr = city.Name
		Db.Table("cat_meal_region").Where("id = ?", value.DistrictId).First(&district)
		value.AreaStr = district.Name
	}
	response.Success(ctx, gin.H{"list": list}, "success")
}

// RemoveAddress 删除地址
func RemoveAddress(ctx *gin.Context) {
	user, _ := ctx.Get("user")
	idStr := ctx.PostForm("id")
	if len(idStr) == 0 {
		response.Fail(ctx, gin.H{}, "缺少地址标识")
		return
	}
	sli := strings.Split(idStr, ",")
	var cart model.CartStruct
	Db := common.GetDB()
	Db.Debug().Table("cat_meal_address").Where("uid = ? and id in (?)", user.(model.UserInfo).Uid, sli).Delete(&cart)
	response.Success(ctx, gin.H{}, "success")
}

func ModifyAddress(ctx *gin.Context) {
	id := ctx.PostForm("id")
	mobile := ctx.PostForm("mobile")
	isDefault := ctx.PostForm("isDefault") //是否默认地址
	linkMan := ctx.PostForm("linkMan")     //联系人
	latitude := ctx.PostForm("latitude")
	longitude := ctx.PostForm("longitude")
	provinceId := ctx.PostForm("provinceId")
	cityId := ctx.PostForm("cityId")
	districtId := ctx.PostForm("districtId")
	address := ctx.PostForm("address")
	if len(linkMan) == 0 || len(address) == 0 || len(provinceId) == 0 || len(cityId) == 0 || len(id) == 0 {
		response.Fail(ctx, gin.H{}, "参数有误")
		return
	}
	Db := common.GetDB()
	user, _ := ctx.Get("user")
	if isDefault != "true" {
		isDefault = "false"
	} else {
		type changeDefault struct {
			IsDefault string `gorm:"column:isDefault"`
		}
		structDefault := changeDefault{
			IsDefault: "false",
		}
		Db.Debug().Table("cat_meal_address").Where("uid = ?", user.(model.UserInfo).Uid).Updates(&structDefault)
	}
	if len(mobile) != 11 {
		response.Fail(ctx, gin.H{}, "手机号码有误")
		return
	}
	var info model.AddressInfo
	Db.Debug().Table("cat_meal_address").Where("id=? and uid =?", id, user.(model.UserInfo).Uid).First(&info)
	if info.Id == 0 {
		response.Fail(ctx, gin.H{}, "记录不存在")
		return
	}
	insert := model.InsertAddressInfo{
		Uid:        user.(model.UserInfo).Uid,
		LinkMan:    linkMan,
		Address:    address,
		Mobile:     mobile,
		IsDefault:  isDefault,
		Latitude:   latitude,
		Longitude:  longitude,
		ProvinceId: provinceId,
		CityId:     cityId,
		DistrictId: districtId,
	}
	Db.Debug().Table("cat_meal_address").Where("id=? and uid =?", id, user.(model.UserInfo).Uid).Updates(&insert)
	response.Success(ctx, gin.H{}, "修改成功")
}
